<?php

namespace app\controllers;

use app\models\Goods;
use app\models\Order;
use Yii;
use yii\debug\models\search\Log;
use yii\web\Controller;
class SeckController extends Controller
{
    public function actionIndex()
    {
        $model = new Goods();
        $data = $model->getAll();
        return $this->render("index",["data"=>$data]);
    }

        //将数据存入redis中
    public function actionRedis()
    {
        $model = new Goods();
        $data = $model->getAll();

        $redis = new \Redis;
        $redis->connect('127.0.0.1','6379');
        $redis->select(10);

        foreach($data as $key=>$value)
        {
            $redis->lLen("goods");
        }
    }


    //实现秒杀
    public function actionIndex_do()
    {
        $redis = new \Redis;
        $redis->connect('127.0.0.1','6379');
        $redis->select(10);

        $key ="goods".$id;

        if($redis->lPush('num')>0)
        {
            //入库
            $model = new Order();
            $rand = ("Ymd").md5(rand(100,9999));
            $goods_id = $id;
            $randtine = ktime;
            $data = $model->getOne($rand,$goods_id,$randtine);

            //入日志
            $model = new Log();
            $uid = "11";
            $goods_id = $id;
            $status="0";
            $data =$model->getOne($uid,$goods_id,$status);


            echo json_encode(['code'=>1,'msg'=>'秒杀成功']);
        }else{
            echo json_encode(['code'=>1,'id'=>$id,'msg'=>'秒杀成功']);
        }
    }
}